package com.bz.sso.controller;

import com.bz.pojo.TbUser;
import com.bz.sso.service.SSOService;
import com.bz.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户注册于登录
 */
@Slf4j
@RestController
@RequestMapping("/sso")
public class SSOController {

    @Autowired
    private SSOService ssoService;

    /**
     * 对用户的注册信息(用户名与电话号码)做数据校验
     * @param checkValue
     * @param checkFlag
     * @return
     */
    @GetMapping("/checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable String checkValue,@PathVariable Integer checkFlag){
        return ssoService.checkUserInfo(checkValue,checkFlag);
    }

    /**
     * 用户注册
     * @param tbUser
     * @return
     */
    @PostMapping("/userRegister")
    public Result userRegister(@RequestBody TbUser tbUser){
        return ssoService.userRegister(tbUser);
    }

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/userLogin")
    public Result userLogin(String username, String password, HttpServletRequest request, HttpServletResponse response){
        return ssoService.userLogin(username,password,request,response);
    }

    /**
     * 用户退出登录
     * @param token
     * @return
     */
    @GetMapping("/logOut")
    public Result logOut(String token){
        try {
            return ssoService.logOut(token);
        } catch (Exception e) {
            log.error("【不管成功还是失败，都应该退出成功】，{}",e);
        }
        return Result.error("退出登录失败");
    }
}
